<html>

<head>
	<meta charset="UTF-8">
	<title>Home</title>
	<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/style.css');?>">
	<link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
	<link href='http://fonts.googleapis.com/css?family=Raleway:300' rel='stylesheet' type='text/css'>
	<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
	<script src="<?php echo base_url('js/jquery.plugin.js');?>"></script>
	<script src="<?php echo base_url('js/jquery.countdown.js');?>"></script>
	<script type="text/javascript">
		var array = <?php echo json_encode($results_give); ?>;

		function a(){
			for (var i = 0; i < array.length; i++)
			{
		        //Criação das divs para colocar o valores obtidos
		        var giveaway = document.createElement('div');
		        giveaway.id = 'giveaway';
		        document.getElementById('wrapper2').appendChild(giveaway);

		        var tipo = document.createElement('h3');
		        giveaway.appendChild(tipo);

		        var imagem = document.createElement('div');
		        imagem.id = 'imagem';
		        giveaway.appendChild(imagem);

		        var entrar = document.createElement('div');
		        entrar.id = 'entrar';
		        giveaway.appendChild(entrar);

		        var tempo_label = document.createElement('h4');
		        giveaway.appendChild(tempo_label);

		        var tempo = document.createElement('h6');
		        tempo.id = 'time' + i;
		        giveaway.appendChild(tempo);

		        var user_label = document.createElement('h4');
		        giveaway.appendChild(user_label);

		        var user = document.createElement('h6');
		        user.id = 'user';
		        giveaway.appendChild(user);

		        var entradas_label = document.createElement('h4');
		        giveaway.appendChild(entradas_label);

		        var entradas = document.createElement('h6');
		        entradas.id = 'entradas' + i;
		        giveaway.appendChild(entradas);

		        //Obter os valores
		        //Tipo
		        if(array[i].G_TIPO){
		            tipo.innerHTML = array[i].G_TIPO;
		        }
		        else
		          tipo.innerHTML = '';
		      	//Imagem
		      	if(array[i].G_IMAGEM){
		            imagem.innerHTML = '<img src="<?php echo base_url()?>/imagens/' + array[i].G_IMAGEM + '"/>';
		        }
		        else
		           imagem.innerHTML = '';
		       	//Botão Entrar
		        entrar.innerHTML = '<input type="submit" name="entrarbtn" id="entrarbtn" value="Entrar" onClick="login()">';
		        //Acaba em: 
		        tempo_label.innerHTML = 'Acaba em: ';
		        //Tempo
		         
		        //Criado por:
		        user_label.innerHTML = 'Criado por: ';
		        //User
		        user.innerHTML = '<a href="">' + array[i].G_AUTOR + '</a>';
		        //Entradas:
		        entradas_label.innerHTML = 'Entradas: ';
		        entradas.innerHTML = '0';
		        listarEntradas(array[i].G_ID);
		        //Entradas
		        entradas.innerHTML = array[i].G_ENTRADAS;
		   }
		   countdown();
		}		

		function listarEntradas(id)
		{
			$.ajax({
			  type: "POST",
			  url: "<?php echo base_url('/giveaways/pesquisaEntradas');?>",
			  data: { "id": id },
			  success: function(msg) {
			  	for(var j=0; j < array.length; j++)
				{
					if(array[j].G_ID == id)
			    		document.getElementById("entradas" + j).innerHTML = msg;
			    	else{}
			    }
			  }
			});
		}

		function countdown() {
			for(var j=0; j < array.length; j++)
			{
				$(function () {
					var austDay = new Date();
					austDay = new Date(array[j].G_DATA_F);
					$('#time' + j).countdown({until: austDay, layout: '{dn}d {hn}h {mn}m {sn}s'});
				});
			}
		}

		function login()
		{
			alert("Efectue login para participar no giveaway.");
		}

	</script>

</head>


<body onload="a()">
	<div class="header">
		<div id="wrapper">
			<div class="logo">
				<h1><a href="<?php echo base_url('/giveaways');?>">GIVEAWAYS</a></h1>
			</div>
			<div class="menu">
				<?php 					
					$this->load->helper("form");

					echo form_open('/giveaways/reg_or_log');

					$data = array (
						"name" => "mail",
						"id" => "mail",
						"value" => "",
						"type" => "email",
						"placeholder" => "E-Mail",
						"onClick" => "if(this.placeholder=='E-Mail')this.placeholder=''",
						"onBlur" => "if(this.placeholder=='')this.placeholder='E-Mail'"
					);
					echo form_input($data);

					$data = array (
						"name" => "pass",
						"id" => "pass",
						"value" => "",
						"type" => "password",
						"placeholder" => "Password",
						"onClick" => "if(this.placeholder=='Password')this.placeholder=''",
						"onBlur" => "if(this.placeholder=='')this.placeholder='Password'"
					);
					echo form_input($data);

					echo form_submit("loginbtn", "Login");
					echo form_submit("registarbtn", "Registar");
					echo validation_errors();

					echo form_close(); 

				?>
			</div>
		</div>
	</div>

	<div class="body">
			<div class="middle">
				<div id="wrapper">
					<h2>RECEBE E OFERECE<br/>O QUE QUISERES</h2>
					<div class="form1">
						<form method="post" action="<?php echo base_url('/giveaways/pesquisa_index_pesquisa');?>">
					       <input type="text" name="search" id="search" placeholder="Pesquisa Giveaways" onClick="if(this.placeholder=='Pesquisa Giveaways')this.placeholder=''" onBlur="if(this.placeholder=='')this.placeholder='Pesquisa Giveaways'">

					       <input type="submit" name="searchbtn" id="searchbtn" value="">
					    </form>
				    </div>
				</div>
			</div>

			<div class="giveaway-list">
				<div id="wrapper2">
				</div>
			</div>
	</div>

</body>
<script type="text/javascript"> 
	alert ('<?php echo $feedback?>');
</script>

</html>